<extend name="./public/frame.html"/>
<block name="content">
    <style>
        .layui-form-label {
            width: 12%;
        }

        .layui-input-block {
            margin-left: 12%;
        }
    </style>
    <script type="text/javascript" charset="utf-8" src="/public/editor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/public/editor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="/public/editor/lang/zh-cn/zh-cn.js"></script>
    <form class="layui-form layui-form-pane1" id="testForm" method="post" enctype="multipart/form-data">
        <table class="layui-table">
            <if $userType==1>
            <tr>
                <td style="text-align: center;">页面是否显示</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="is_display" name="is_display" class="layui-input" lay-filter="is_display"
                                style="width: 500px;">
                            <option value="1">显示</option>
                            <option value="2">不显示</option>
                        </select>
                    </div>
                </td>
            </tr>
            </if>
            <tr>
                <td style="text-align: center;width: 10%">活动名称</td>
                <td><input type="text" name="activity_name" id="activity_name" lay-verify="required" placeholder="活动名称"
                           class="layui-input" style="width: 500px;"></td>
            </tr>
            <if $userType==1>
            <tr>
                <td style="text-align: center;">活动banner</td>
                <td>
                    <img id="pic_url" layer-src="" src="" width="100px;" height="100px;" style="display: none;">&nbsp;&nbsp;<button
                        type="button" class="layui-btn uploads" id="upload-CarBrand}">上传图片
                </button>
                    <input name="activity_img" id="activity_img" type="hidden" value="">
                </td>
            </tr>
            </if>
            <if $userType==1>
            <tr>
                <td style="text-align: center;">活动Url</td>
                <td><input type="text" name="activity_url" id="activity_url" lay-verify="required"
                           placeholder="请填写https://或http://开头的URL地址" class="layui-input" style="width: 500px;"></td>
            </tr>
            </if>
            <tr>
                <td style="text-align: center;">活动时间</td>
                <td>
                    <input type="text" name="start_time" id="start_time" lay-verify="required" placeholder="活动开始时间"
                           class="layui-input" style="width: 150px;float: left;margin-right: 10px;"><span
                        style="float: left;margin-right: 10px;padding-top: 5px;">-</span>
                    <input type="text" name="end_time" id="end_time" lay-verify="required" placeholder="活动结束时间"
                           class="layui-input" style="width: 150px;float: left;">
                </td>
            </tr>
<!--            <tr>-->
<!--                <td style="text-align: center;">活动对象</td>-->
<!--                <td>-->
<!--                    <div class="layui-input-inline" style="width: 250px;">-->
<!--                        <select id="user_type" name="user_type" class="layui-input" lay-filter="user_type"-->
<!--                                style="width: 500px;">-->
<!--                            <?php foreach($terminal as $key=>$value):?>-->
<!--                            <option value="<?php echo $key;?>"><?php echo $value;?></option>-->
<!--                            <?php endforeach;?>-->
<!--                        </select>-->
<!--                    </div>-->
<!--                </td>-->
<!--            </tr>-->
            <input type="hidden" value="{$userType}" name="user_type">
            <tr>
                <td style="text-align: center;">活动类型</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="activity_model" name="activity_model" class="layui-input"
                                lay-filter="activity_model" style="width: 500px;">
                            <?php foreach($acitivetyType as $key=>$value):?>
                            <if $userType!=1>
                            <if in_array($key,[7,2,8,6,10,11,12])>
                            <option value="<?php echo $key;?>"
                                    id="hdlx_<?php echo $key;?>"><?php echo $value;?></option>
                            </if>
                                <else/>
                                <if in_array($key,[9,1,5,3,6,10,11,12])>
                                    <option value="<?php echo $key;?>"
                                            id="hdlx_<?php echo $key;?>"><?php echo $value;?></option>
                                </if>
                            </if>
                            <?php endforeach;?>
                        </select>
                    </div>
                </td>
            </tr>
            <!--------------------------------------------------------------------------针对活动奖励设置处理------------------------------------------------------------------->
            <tr class="activityType_enjoy" style="display: none;">
                <td style="text-align: center;">第几个红包最大奖励</td>
                <td>
                    <input class="layui-input" type="text" name="activity_enjoy" id="activity_enjoy" placeholder="填数字，最大999"
                           style="width: 500px" onkeyup="num(this)">
                </td>
            </tr>
            <tr class="activityType_enjoy" style="display: none;">
                <td style="text-align: center;">最大个人发送多少份红包</td>
                <td>
                    <input class="layui-input" type="text" name="enjoy_send" id="enjoy_send" placeholder="填数字，最大999"
                           style="width: 500px" onkeyup="num(this)">
                </td>
            </tr>
            <tr class="activityType_enjoy" style="display: none;">
                <td style="text-align: center;">最大个人领取多少份红包</td>
                <td>
                    <input class="layui-input" type="text" name="enjoy_receive" id="enjoy_receive" placeholder="填数字，最大999"
                           style="width: 500px" onkeyup="num(this)">
                </td>
            </tr>
            <tr class="activityType_sz" style="display: none;">
                <td style="text-align: center;">奖励设置</td>
                <td>
                    <table class="layui-table" style="width: 100%;">
                        <tr>
                            <td style="width: 15%;text-align: center;">在线时长</td>
                            <td>
                                <input class="layui-input" type="text" name="second" value="" id="second"
                                       placeholder="在线时长（单位/小时）" style="width: 500px" onkeyup="num(this)">
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 15%;text-align: center;">在线期间完成单数</td>
                            <td>
                                <input class="layui-input" type="text" name="order_sum" value="0" id="order_sum"
                                       placeholder="订单数量" style="width: 500px"
                                       onkeyup="value=value.replace(/[^\d]/g,'')">
                            </td>
                        </tr>
                        <tr>
                            <td style="width: 15%;text-align: center;">活动时间段</td>
                            <td>
                                <input type="text" name="start_time_interval" id="start_time_interval"
                                       placeholder="起始时间（时分）" class="layui-input LAY_demorange_s"
                                       style="width: 150px;float: left;margin-right: 10px;"><span
                                    style="float: left;margin-right: 10px;padding-top: 5px;">-</span>
                                <input type="text" name="end_time_interval" id="end_time_interval"
                                       placeholder="终止时间（时分）" class="layui-input LAY_demorange_e"
                                       style="width: 150px;float: left;">
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>

            <tr class="activityType_00" <if condition="$userType!=1"> style="display: none;"</if>>
                <td style="text-align: center;">奖励类型</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="award_type" name="award_type" class="layui-input" lay-filter="award_type"
                                style="width: 500px;">
                            <?php foreach($award_type as $key=>$value):?>
                            <option value="<?php echo $key;?>"><?php echo $value;?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </td>
            </tr>
            <tr class="activityType_01" style="display: none">
                <td style="text-align: center;">活动奖励</td>
                <td>
                    <input class="layui-input" type="text" name="amount" value="" id="amount" placeholder="活动奖励(元/积分)"
                           style="width: 500px" onkeyup="num(this)">
                </td>
            </tr>

            <tr class="activityType_02" style="">
                <td style="text-align: center;">活动奖励</td>
                <td>
                    <table class="layui-table inside_price" style="width: 500px;">
                        <tr>
                            <td style="text-align: center;">订单数量(完成)</td>
                            <td style="text-align: center;">奖励金额(元)&nbsp;&nbsp;&nbsp;&nbsp;<a
                                    class="layui-btn layui-btn-xs layui-btn-warm" lay-event="look" style="width: 30px"
                                    onclick="insidePrice()">+</a></td>
                        </tr>
                        <tr>
                            <td><input type="text" class="layui-input" lay-verfify="required" name="orderNum[]"
                                       id="orderNum1" placeholder="订单数量" onkeyup="num(this)"></td>
                            <td><input type="text" class="layui-input" lay-verfify="required" name="money[]" id="money1"
                                       placeholder="奖励金额(元)" onkeyup="num(this)"></td>
                        </tr>
                    </table>
                    <table class="layui-table" style="width: 500px;">
                        <tr>
                            <td style="width: 25%;text-align: center;">活动时间段</td>
                            <td>
                                <input type="text" name="order_start_time_interval" id="order_start_time_interval"
                                       placeholder="起始时间（时分）" class="layui-input LAY_demorange_start"
                                       style="width: 150px;float: left;margin-right: 10px;"><span
                                    style="float: left;margin-right: 10px;padding-top: 5px;">-</span>
                                <input type="text" name="order_end_time_interval" id="order_end_time_interval"
                                       placeholder="终止时间（时分）" class="layui-input LAY_demorange_end"
                                       style="width: 150px;float: left;">
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr class="activityType_day">
                <td style="text-align: center;">奖励限制</td>
                <td>
                    <input class="layui-input" type="text" name="limit_days" value="" id="limit_days"
                           placeholder="N天数完成 - 奖励" style="width: 500px" onkeyup="num(this)">
                </td>
            </tr>
            <tr class="activityType_03">
                <td style="text-align: center;">奖励发放形式</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="hand_out_type" name="hand_out_type" class="layui-input" lay-filter="hand_out_type"
                                style="width: 500px;">
                            <option value="0">实时发放</option>
                            <option value="1">定时发放</option>
                        </select>
                    </div>
                </td>
            </tr>
            <tr class="activityType_04" style="display: none;">
                <td style="text-align: center;">定时发放</td>
                <td>
                    <input class="layui-input" type="text" name="hours" id="hours" value="" placeholder="多少小时后发放（请输入数字）"
                           style="width: 500px" onkeyup="num(this)">
                </td>
            </tr>
            <!--------------------------------------------------------------------------针对司机活动奖励设置处理------------------------------------------------------------------->

            <tr>
                <td style="text-align: center;">所属活动</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="order_type" name="order_type" class="layui-input" lay-filter="order_type"
                                style="width: 500px;">
                            <?php foreach($order_type as $key=>$value):?>
                            <option value="<?php echo $key;?>"><?php echo $value;?></option>
                            <?php endforeach;?>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;">选择城市</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="city_type" name="city_type" lay-filter="city_type" class="layui-input"
                                style="width: 500px;">
                            <option value="0">全国</option>
                            <option value="1">城市</option>
                        </select>
                    </div>
                </td>
            </tr>
            <tr class="ts_city" style="display: none;">
                <td style="text-align: center;">推送城市</td>
                <td>
                    <div class="layui-form-item" style="margin-bottom: 0px;">
                        <input type="checkbox" data-type="all" title="全部" lay-verify="required|region">
                        <div id="region">
                            <volist name="list" id="v">
                                <input type="checkbox" name="region[]" title="{$v.zone_name}" value="{$v.city_id}" class="item_child" style="margin-top: 5000px;!important;">
                            </volist>
                        </div>
                    </div>
                </td>
            </tr>
            <tr style="display: none;">
                <td style="text-align: center;">奖励发放截止类型</td>
                <td>
                    <input type="radio" name="end_date_model" lay-filter="end_date_model" value="1" title="固定日期"
                           checked>
                    <input type="radio" name="end_date_model" lay-filter="end_date_model" value="2" title="非固定日期">
                </td>
            </tr>
            <tr style="display: none;">
                <td style="text-align: center;">奖励发放截止时间</td>
                <td>
                    <input type="text" name="date_time" id="date_time" placeholder="日期(非必填=》针对邀请车主认证)"
                           class="layui-input" style="width: 250px;float: left;margin-right: 10px;">
                    <input type="number" name="date_days" id="date_days" placeholder="天数(非必填=》针对邀请车主认证)"
                           class="layui-input" style="width: 250px;float: left;margin-right: 10px;display: none;">
                </td>
            </tr>
            <tr>
                <td style="text-align: center;">活动描述</td>
                <td>
                    <textarea placeholder="请输入活动描述" class="layui-textarea" name="remark" lay-verify="required"
                              style="width: 500px;"></textarea>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;">活动规则</td>
                <td>
                    <script name="rule" id="editor" type="text/plain" style="width:960px;height:300px;"></script></div>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="padding-left: 100px;">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
            </tr>
        </table>
    </form>
    <script>
        layui.use(['form', 'upload', 'laydate'], function () {
            form = layui.form, upload = layui.upload;
            var laydate = layui.laydate;
            laydate.render({
                elem: '#start_time'
                , min: '<?php echo date("Y-m-d",strtotime("+1 day"));?>'
            });
            laydate.render({
                elem: '#end_time'
                , min: '<?php echo date("Y-m-d",strtotime("+1 day"));?>'
            });
            laydate.render({
                elem: '#date_time'
                , min: '<?php echo date("Y-m-d",strtotime("+1 day"));?>'
            });
            laydate.render({
                elem: '.LAY_demorange_s',
                type: 'time',
                format: 'H:mm'
            });
            laydate.render({
                elem: '.LAY_demorange_e',
                type: 'time',
                format: 'H:mm'
            });
            laydate.render({
                elem: '.LAY_demorange_start',
                type: 'time',
                format: 'H:mm'
            });
            laydate.render({
                elem: '.LAY_demorange_end',
                type: 'time',
                format: 'H:mm'
            });
            upload.render({
                elem: '.uploads'//这个就等同于点击
                , url: '/admin/Upload/uploadImg'
                , done: function (res) { //上传后的回调

                    console.log(res)
                    if (res.status == 1) {
                        $("#pic_url").css('display', '');
                        $("#pic_url").attr('src', res.data.file); //点击后的大图
                        $("#pic_url").attr('layer-src', res.data.file_thumb); //小图
                        $("#activity_img").val(res.data.file); //点击后的大图
                        layer.msg('上传成功', {icon: 1});
                    } else {
                        layer.msg(res.msg);
                    }
                }
                , error: function (e) {
                    console.log(e)
                }
            });
            var user_type = $("#user_type").val();              //活动对象
            //活动对象
            form.on('select(user_type)', function (data) {
                $("#activity_model").val("1");                       //活动类型初始化重新选择
                $("#hand_out_type").val("0");                        //发放形式初始化重新选择
                $("#award_type").val("1");
                $('.activityType_01').css('display', '');            //活动奖励1
                $('.activityType_02').css('display', 'none');        //活动奖励2
                $('.activityType_03').css('display', '');            //奖励发放形式
                $('.activityType_04').css('display', 'none');        //奖励发放时间
                if (data.value == 1) {
                    $('.activityType_00').css('display', '');        //奖励类型
                    $('#hdlx_2').attr("disabled", 'disabled');
                    $('.activityType_day').css('display', '');
                    $('.activityType_enjoy').css('display', '');

                } else {
                    $('.activityType_00').css('display', 'none');    //奖励类型
                    $('#hdlx_2 ').removeAttr("disabled");
                }

                $('.activityType_sz').css('display', 'none');
                $('.activityType_enjoy').css('display', 'none');
                form.render('select');
            });
            //活动类型
            form.on('select(activity_model)', function (data) {
                console.log(data.value)
                $("#award_type").val("1");
                if (data.value == 2) {
                    $('.activityType_01').css('display', 'none');
                    $('.activityType_02').css('display', '');

                } else {
                    $('.activityType_01').css('display', '');
                    $('.activityType_02').css('display', 'none');
                }
                if (data.value == 4) {
                    //$(".activityType_day").css('display','');
                } else {
                    //$(".activityType_day").css('display','none');
                }
                if ((data.value == 7 || data.value == 8) ) {
                    $('.activityType_day').css('display', 'none');
                    $('.activityType_sz').css('display', '');
                    $('.activityType_enjoy').css('display','none');
                } else if (data.value == 2 ) {
                    $('.activityType_day').css('display', 'none');
                    $('.activityType_sz').css('display', 'none');
                    $('.activityType_enjoy').css('display','none');

                } else if (data.value == 9) {
                    $('.activityType_enjoy').css('display','');
                    $('.activityType_day').css('display', 'none');
                    $('.activityType_sz').css('display', 'none');

                } else {
                    $('.activityType_day').css('display', '');
                    $('.activityType_sz').css('display', 'none');
                    $('.activityType_enjoy').css('display','none');
                }
                form.render('select');
            });
            //奖励发放
            form.on('select(hand_out_type)', function (data) {
                if (data.value == 1) {
                    $(".activityType_04").css('display', '');
                } else {
                    $(".activityType_04").css('display', 'none');
                }
            });
            //奖励类型
            form.on('select(award_type)', function (data) {
                //扩展所用针对优惠券 乘客（订单奖励-老用户）
                if (data.value == 1 || data.value == 3) {
                    $('.activityType_01').css('display', '');            //活动奖励1
                    $('.activityType_02').css('display', 'none');        //活动奖励2
                    $('.activityType_03').css('display', '');            //奖励发放形式
                    $('.activityType_04').css('display', 'none');        //奖励发放时间
                    $('.activityType_day').css('display', '');           //奖励限制
                } else {
                    $('.activityType_01').css('display', 'none');        //活动奖励1
                    $('.activityType_02').css('display', 'none');        //活动奖励2
                    $('.activityType_03').css('display', 'none');        //奖励发放形式
                    $('.activityType_04').css('display', 'none');        //奖励发放时间
                    $('.activityType_day').css('display', 'none');       //奖励限制
                }
            });
            //选择城市
            form.on('select(city_type)', function (data) {
                if (data.value == 1) {
                    $(".ts_city").css('display', '')
                } else {
                    $(".ts_city").css('display', 'none')
                }
            });
            //根据类型加载城市
            form.on('select(order_type)', function (data) {
                var type = data.value;
                $.post('/admin/coupon/get_open_region', {'type': type}, function (res) {
                    $("#region").html(res)
                    form.render();
                })
            });
            //推送城市
            form.on('checkbox', function (data) {
                var type = $(this).data('type');
                if (type == 'all') {
                    var child = $('#region').find('.item_child');
                    child.each(function (index, item) {
                        item.checked = data.elem.checked;
                    });
                    form.render('checkbox');
                }
            });
            //截止日期选择
            form.on('radio(end_date_model)', function (data) {
                if (data.value == 1) {
                    $("#date_time").css('display', '');
                    $("#date_days").css('display', 'none');
                } else if (data.value == 2) {
                    $("#date_time").css('display', 'none');
                    $("#date_days").css('display', '');
                }
            });
            form.on('submit(*)', function (data) {
                //活动时间比较
                var start_time = $("#start_time").val();
                var end_time = $("#end_time").val();
                if (start_time > end_time) {
                    layer.msg('活动开始时间不能大于结束时间', {icon: 5});
                    return false
                }
                var user_type = $("#user_type").val();
                var activity_model = $("#activity_model").val();
                if (user_type == 2) {
                    if (activity_model == 7 || activity_model == 8) {
                        var start_time_interval = $("#start_time_interval").val();
                        var end_time_interval = $("#end_time_interval").val();
                        var st = start_time_interval.split(':');
                        var et = end_time_interval.split(':');
                        if ((st[0] + st[1]) * 1 > (et[0] + et[1]) * 1) {
                            layer.msg('活动时间段时间不能大于结束时间', {icon: 5});
                            return false
                        }
                    } else if (activity_model == 2) {
                        var order_start_time_interval = $("#order_start_time_interval").val();
                        var order_end_time_interval = $("#order_end_time_interval").val();
                        var st = order_start_time_interval.split(':');
                        var et = order_end_time_interval.split(':');
                        if ((st[0] + st[1]) * 1 > (et[0] + et[1]) * 1) {
                            layer.msg('活动时间段时间不能大于结束时间', {icon: 5});
                            return false
                        }
                    }
                }
                var activity_img = $("#activity_img").val();
                // if (activity_img == '') {
                //     layer.msg('活动banner图不能为空', {icon: 5});
                //     return false
                // }
                var activity_url = $("#activity_url").val();
                var reg = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
                // if (!reg.test(activity_url)) {
                //     layer.msg('请输入正确网址链接', {icon: 5});
                //     return false
                // }
                var user_type = $("#user_type").val();              //活动对象
                var activity_model = $("#activity_model").val();    //活动类型
                if (user_type == 1) {                                 //活动对象乘客
                    var award_type = $("#award_type").val();        //奖励方式 1现金2优惠券
                    if (award_type == 1) {
                        if (activity_model != 2) {
                            var amount = $("#amount").val();
                            if (amount == '') {
                                // layer.msg('奖励金额不能为空', {icon: 5});
                                // return false
                            }
                        }
                        var hand_out_type = $("#hand_out_type").val();
                        if (hand_out_type == 1) {
                            var hours = $("#hours").val();
                            if (hours == '') {
                                layer.msg('设置定时时间不能为空', {icon: 5});
                                return false
                            }
                        }
                    }
                } else {
                    if (activity_model != 2) {
                        var amount = $("#amount").val();
                        if (amount == '') {
                            // layer.msg('奖励金额不能为空', {icon: 5});
                            // return false
                        }
                    } else {
                        //var complete_order_num = $("#complete_order_num").val();
                        //if(complete_order_num < 0){
                        //    layer.msg('完成订单数量不能小于0',{icon: 5});return false
                        //}
                    }
                    var hand_out_type = $("#hand_out_type").val();
                    if (hand_out_type == 1) {
                        var hours = $("#hours").val();
                        if (hours == '') {
                            layer.msg('设置定时时间不能为空', {icon: 5});
                            return false
                        }
                    }
                }
                var end_date_model = $("input[name='end_date_model']:checked").val();
                if (end_date_model == 2) {
                    if ($("#date_days").val() < 0 || $("#date_days").val() == '') {
                        //layer.msg('天数不能为空或小于0',{icon: 5});return false
                    }
                } else {
                    if ($("#date_time").val() == '') {
                        //layer.msg('日期不能为空',{icon: 5});return false
                    }
                }
            });
        });

        //验证只能输入数字
        function num(object) {
            var value = object.value;
            var id = object.id;
            if (isNaN(value)) {
                layer.msg('请输入正确的数字', {icon: 5});
                $("#" + id).val('');
                return false;
            }
        }

        //订单奖励累计
        function insidePrice() {
            var i = $('.inside_price tr').length - 1;
            i++;
            var html = '<tr>' +
                '           <td><input type="text" class="layui-input" lay-verfify="required" name="orderNum[]" id="orderNum' + i + '" placeholder="订单数量" onkeyup="num(this)"></td>\n' +
                '           <td>\n' +
                '               <input type="text" class="layui-input" lay-verfify="required" name="money[]" id="money' + i + '" placeholder="奖励金额(元)" onkeyup="num(this)" style="width:75%;margin-right:10px;float: left; ">\n' +
                '               <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="look" style="width: 30px;margin-top: 8px;" onclick="reduceInsidePrice(this)">-</a>\n' +
                '          </td>' +
                '       </tr>';
            $(".inside_price").append(html);
        }

        //订单奖励 减少
        function reduceInsidePrice($this) {
            $($this).parent().parent().remove();
        }
    </script>
    <script type="text/javascript">
        var ue = UE.getEditor('editor',{
            zIndex : 0,// z轴
        });
    </script>
</block>
